﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    ui->caculate_node->setFlat(true);
    ui->stackedWidget->setCurrentIndex(0);
    ui->caculate_tree->header()->setStyleSheet("QHeaderView::section{background:rgb(255, 255, 255,0);}");
    ui->caculate_tree->setStyleSheet("background-color: rgba(255, 255, 255, 75);");
    ui->task_widget->setStyleSheet("background-color: rgba(255, 255, 255, 75);");
    ui->task_widget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(255, 255, 255,0);}");
    this->setStyleSheet("#MainWindow{border-image:url(:/resource/p1.jpg)}");
    ui->DLL_lineEdit->setFocusPolicy(Qt::NoFocus);    //禁止dll路径框编辑
    ui->task_lineEdit->setFocusPolicy(Qt::NoFocus);   //禁止任务文件路径
    Mysetting=new Setting();
    Mysetting->init("manage_server");
    Mysetting->Start();
    socket=new tcp_socket();
    socket_thread=new QThread();
    connect(this,SIGNAL(socket_senddata(QString,QString,QString)),socket,SLOT(send_data(QString,QString,QString)));
    connect(socket,SIGNAL(reply_data(QStringList)),this,SLOT(get_information(QStringList)));
    connect(socket,SIGNAL(socket_isconnect(bool)),this,SLOT(Socket_type(bool)));
    socket->ConnectToServer();
    socket_thread->start();
    socket->moveToThread(socket_thread);
    /*login  *login1=new login();
    connect(socket,SIGNAL(reply_data(QStringList)),login1,SLOT(receive_data(QStringList)));
    connect(login1,SIGNAL(socket_senddata(QString,QString,QString)),socket,SLOT(send_data(QString,QString,QString)));
    connect(login1,SIGNAL(socket_setpassword(QString)),socket,SLOT(setpassword(QString)));
    connect(login1,SIGNAL(set_id_password(QString,QString)),this,SLOT(get_id_password(QString,QString)));
    login1->setModal(true);
    login1->setsocket(socket);
    int result=login1->exec();
    if(result!=1)
    {
        exit(0);
    }
    else
    {*/
    this->show();
    showtime();
    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::showtime);
    timer->start(1000);
    update=new QTimer(this);
    connect(update,&QTimer::timeout,this,&MainWindow::update_data);
    update->start(10000);
    ui->task_widget->setColumnWidth(0,100);
    ui->task_widget->setColumnWidth(2,80);
    ui->task_widget->setColumnWidth(4,130);
    ui->task_widget->setColumnWidth(5,130);
    //  }


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showtime()
{
    QDate date=QDate::currentDate();
    QString now_date=date.toString("yyyy-MM-dd");
    QTime clock=QTime::currentTime();
    QString txttime=clock.toString("hh:mm:ss");
    ui->lcdNumber->display(now_date+" "+txttime);
}



void MainWindow::on_caculate_node_clicked()
{
    this->setStyleSheet("#MainWindow{border-image:url(:/resource/p1.jpg)}");
    ui->stackedWidget->setCurrentIndex(0);
}



void MainWindow::on_task_data_clicked()
{
    this->setStyleSheet("#MainWindow{border-image:url(:/resource/p3.jpg)}");
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_new_task_clicked()
{
    this->setStyleSheet("#MainWindow{border-image:url(:/resource/p2.jpg)}");
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::updata_caculate_information(QString data)
{


    tree_List.clear();

    QStringList information;
    if(data.size()>0)
        information=data.split("$");

    ui->caculate_tree->setHeaderLabel("在线计算节点 : "+QString::number(information.size()));
    for(int a=0;a<information.size();a++)
    {

        QStringList item_information=information[a].split("#");
        QTreeWidgetItem *imageItem1 = new QTreeWidgetItem;


        imageItem1->setText(0,item_information[0]);
        tree_List.append(imageItem1);
        for(int b=1;b<item_information.size();b++)
        {
            QStringList i;
            i.append(item_information[b]);
            if(b!=5)
            {
                QTreeWidgetItem *imageItem2 = new QTreeWidgetItem(imageItem1,i);   //添加子节点
                imageItem1->addChild(imageItem2);
            }
            else
            {
                QStringList node_type=i.first().split("@");
                QTreeWidgetItem *imageItem2 = new QTreeWidgetItem(imageItem1,QStringList("计算线程状态:"+node_type.join(" ")));   //添加子节点
                imageItem1->addChild(imageItem2);



            }

        }

    }
    ui->caculate_tree->clear();
    ui->caculate_tree->insertTopLevelItems(0,tree_List);  //将结点插入部件中
    ui->caculate_tree->expandAll();
}



void MainWindow::caculatenode_information()
{
    emit socket_senddata(user_id,"管理端获取节点信息","");
}

void MainWindow::get_id_password(QString id, QString password)
{
    this->user_id=id;
    this->password=password;
}

void MainWindow::get_information(QStringList data)
{
    QString id,type,information;
    qDebug()<<data<<"!!!!!";
    if(data.size()>=3)
    {
        id=data[0];
        type=data[1];
        information=data[2];
        if(type=="管理端获取节点信息")
        {
            updata_caculate_information(information);
        }
        else if(type=="查询任务")
        {
            output_task(information);
        }
        else if(type=="获取程序类型")
        {
            emit socket_senddata(this->user_id,"程序类型","管理端");
        }
        else if(type=="DLL效验")
        {
            ui->DLL_name->clear();
            QStringList DLL=information.split("@@");
            for(int a=0;a<DLL.size();a++)
            {
                QStringList DLLinformation=DLL[a].split("$$");
                if(DLLinformation.size()!=2)
                {
                    qDebug()<<"获取DLL信息异常";
                    continue;
                }
                qDebug()<<DLLinformation[0];

                ui->DLL_name->insertItem(a,DLLinformation[0]);

            }
        }
        else if(type!="获取计算节点信息")
        {
            qDebug()<<type;
            QMessageBox::about(NULL, "提示", information);
            on_search_task_clicked();
        }
    }
}

void MainWindow::on_commit_task_clicked()
{
    QFile file(TaskPath);

    if(!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(this,tr("不是有效的任务文件"),tr("不是有效的任务文件!"));
        return;
    }
    QTextStream rstream( &file );
    QString Taskdata=rstream.readAll();
    file.close();
    if(ui->DLL_name->currentText().isEmpty())
    {
        QMessageBox::information(this,tr("请选择DLL"),tr("请选择DLL!"));
        return;
    }
    QString data1=ui->DLL_name->currentText()+"&&&!@@@"+Taskdata;
    //   qDebug()<<fi.fileName()<<data.size();
    emit socket_senddata(user_id,"传输任务文件",data1);
}

void MainWindow::on_search_task_clicked()
{
    if(ui->caculating_choice->isChecked())
        emit socket_senddata(user_id,"查询任务","正在运算的任务");
    else
        emit socket_senddata(user_id,"查询任务","");
}

void MainWindow::on_last_page_clicked()
{
    if(caculate_page>1)
    {
        caculate_page--;
        int maxpage=0;
        if(caculate_task_information.size()%caculate_column!=0)
            maxpage=(caculate_task_information.size()/caculate_column)+1;
        else
            maxpage=caculate_task_information.size()/caculate_column;
        QString page_output=QString("%1 / %2").arg(caculate_page).arg(maxpage);
        ui->page->setText(page_output);
        ui->task_widget->setRowCount(caculate_column);

        for(int a=(caculate_page-1)*caculate_column;a<(caculate_page)*caculate_column;a++)
        {
            for(int b=0;b<caculate_task_information[0].size();b++)
            {
                ui->task_widget->setItem(a-(caculate_page-1)*caculate_column,b,new QTableWidgetItem(caculate_task_information[a][b]));
            }
        }
    }

}

void MainWindow::on_next_page_clicked()
{


    int maxpage=0;
    if(caculate_task_information.size()%caculate_column!=0)
        maxpage=(caculate_task_information.size()/caculate_column)+1;
    else
        maxpage=caculate_task_information.size()/caculate_column;
    if(maxpage>caculate_page)
    {
        caculate_page++;

        QString page_output=QString("%1 / %2").arg(caculate_page).arg(maxpage);
        ui->page->setText(page_output);
        for(int a=0;a<caculate_column;a++)
            ui->task_widget->removeRow(0);


        int caculate_size=0;
        if(caculate_task_information.size()<(caculate_page+1)*caculate_column)
        {
            caculate_size=caculate_task_information.size();
        }
        else
        {
            caculate_size=(caculate_page+1)*caculate_column;
        }
        if(maxpage!=caculate_page)
            ui->task_widget->setRowCount(caculate_column);
        else
            ui->task_widget->setRowCount(caculate_size-(caculate_page-1)*caculate_column);
        //qDebug()<<caculate_size<<(caculate_page-1)*caculate_column;
        for(int a=(caculate_page-1)*caculate_column;a<caculate_size;a++)
        {
            for(int b=0;b<caculate_task_information[0].size();b++)
            {
                ui->task_widget->setItem(a-(caculate_page-1)*caculate_column,b,new QTableWidgetItem(caculate_task_information[a][b]));

            }
        }

        ui->task_widget->show();
    }

}

void MainWindow::update_data()
{
    if(ui->caculate_task_update->isChecked()==true)
    {
        on_search_task_clicked();
    }
    if(ui->caculate_node_information_update->isChecked()==true)
    {
        caculatenode_information();
    }
}

void MainWindow::on_search_caculate_information_clicked()
{
    caculatenode_information();
}

void MainWindow::on_task_widget_cellClicked(int row, int column)
{
    target_data=ui->task_widget->item(row,0)->text();

}

void MainWindow::on_delete_task_clicked()
{
    qDebug()<<target_data;
    if(target_data.isEmpty())
    {
        QMessageBox::critical(NULL, "失败", "没有选择删除的数据", QMessageBox::Yes , QMessageBox::Yes);
    }
    else
    {

        QMessageBox::StandardButton rb = QMessageBox::question(NULL, "删除任务", "是否删除任务"+target_data, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(rb == QMessageBox::Yes)
        {
            emit socket_senddata(user_id,"删除任务",target_data);
            target_data.clear();
        }
    }
}

void MainWindow::output_task(QString information)
{
    caculate_page=1;
    caculate_task_information.clear();
    QStringList data1=information.split("#!");
    for(int a=0;a<data1.size();a++)
    {
        caculate_task_information.append(data1[a].split(",#"));
    }
    int maxpage=0;
    if(caculate_task_information.size()%caculate_column!=0)
        maxpage=(caculate_task_information.size()/caculate_column)+1;
    else
        maxpage=caculate_task_information.size()/caculate_column;

    QString page_output=QString("%1 / %2").arg(caculate_page).arg(maxpage);
    ui->page->setText(page_output);
    if(information.isEmpty())
    {

        ui->task_widget->setRowCount(0);
        return ;
    }
    int caculate_size=0;
    if(caculate_task_information.size()<(caculate_page+1)*caculate_column)
    {
        caculate_size=caculate_task_information.size();
    }
    else
    {
        caculate_size=(caculate_page+1)*caculate_column;
    }
    if(maxpage!=caculate_page)
        ui->task_widget->setRowCount(caculate_column);
    else
        ui->task_widget->setRowCount(caculate_size-(caculate_page-1)*caculate_column);


    //qDebug()<<caculate_size<<(caculate_page-1)*caculate_column;
    for(int a=(caculate_page-1)*caculate_column;a<caculate_size;a++)
    {
        for(int b=0;b<caculate_task_information[0].size();b++)
        {
            ui->task_widget->setItem(a-(caculate_page-1)*caculate_column,b,new QTableWidgetItem(caculate_task_information[a][b]));
            ui->task_widget->show();
        }
    }
    ui->task_widget->show();
}

void MainWindow::on_reset_task_clicked()
{
    if(target_data.isEmpty())
    {
        QMessageBox::critical(NULL, "失败", "没有选择重置的数据", QMessageBox::Yes , QMessageBox::Yes);
    }
    else
    {

        QMessageBox::StandardButton rb = QMessageBox::question(NULL, "重置任务", "是否重置任务"+target_data, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(rb == QMessageBox::Yes)
        {
            emit socket_senddata(user_id,"重置任务",target_data);
        }
    }
}

void MainWindow::on_Executive_task_clicked()
{
    QMessageBox::StandardButton rb = QMessageBox::question(NULL, "执行任务", "是否执行未完成任务", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(rb == QMessageBox::Yes)
    {
        emit socket_senddata(user_id,"执行任务","");
    }
}

void MainWindow::on_choice_DLL_clicked()
{
    QString filename;

    filename = QFileDialog::getOpenFileName(this,tr("选择DLL"),"",tr("DLL (*.dll)")); //选择路径
    if(filename.isEmpty())
    {
        return;
    }
    else
    {
        QLibrary lib(filename);
        if(!lib.load())
        {
            QMessageBox::information(this,tr("不是有效的DLL"),tr("不是有效的dll!"));
            return;
        }
        lib.unload();
        DllPath=filename;
        ui->DLL_lineEdit->setText(filename);
    }
}

void MainWindow::on_choice_task_clicked()
{
    QString filename;

    filename = QFileDialog::getOpenFileName(this,tr("选择任务文件"),"",tr("txt (*.txt)")); //选择路径
    if(filename.isEmpty())
    {
        return;
    }
    else
    {
        QFile file(filename);
        if(!file.open(QIODevice::ReadOnly))
        {
            QMessageBox::information(this,tr("不是有效任务文件"),tr("不是有效任务文件!"));
            return;
        }
        file.close();
        TaskPath=filename;
        ui->task_lineEdit->setText(filename);
    }
}

void MainWindow::on_commit_DLL_clicked()
{
    QFile file(DllPath);

    if(!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(this,tr("不是有效的DLL"),tr("不是有效的dll!"));
        return;
    }
    QByteArray dllData= file.readAll();
    file.close();

    //qDebug()<<dllData.size()<<qCompress(dllData).size()<<qCompress(dllData).toBase64().size();
    //QByteArray compress_data=qCompress(dllData);
    QString data=dllData.toBase64();
    QFileInfo fi(DllPath);
    QString data1=fi.fileName()+"&&@@"+data;


    qDebug()<<fi.fileName()<<data.size();
    emit socket_senddata(user_id,"传输DLL",data1);
}

QString MainWindow::Md5(QByteArray data)
{
    QByteArray ba = QCryptographicHash::hash(data, QCryptographicHash::Md5);
    return ba.toHex().constData();
}

QString MainWindow::Md5(QString data)
{
    QByteArray byteArray;
    byteArray=data.toLatin1();
    QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
    QString strMD5 = hash.toHex();
    return strMD5;
}

void MainWindow::on_refresh_DLL_clicked()
{
    emit socket_senddata(user_id,"DLL效验","");
}


void MainWindow::Socket_type(bool connect)
{
    if(connect)
    {
        ui->socket_type->setText("服务器已连接");
        ui->socket_type->setStyleSheet("color:green;");
    }
    else
    {
        qDebug()<<"233";
        ui->socket_type->setText("服务器断开连接,重连中");
        ui->socket_type->setStyleSheet("color:red;");
    }
}
